<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
               
                <el-form-item label="下料数量" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入下料数量" />
                    <div class="form-tips">最多可下{{ formData.allnum - formData.xialiao_num}}</div>
                </el-form-item>
              
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="ordersXialiaoEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiOrdersXialiaoAdd, apiOrdersXialiaoEdit, apiOrdersXialiaoDetail } from '@/api/orders/orders_xialiao'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑下料记录' : '新增下料记录'
})

// 表单数据
const formData = reactive({
    id: '',
    order_id: '',
    detail_id: '',
    num: '',
    allnum:0,
    xialiao_num:0,
    create: userStore.userInfo.name,
})
const checknum = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入数量'))
  } else {
    if (value > formData.allnum - formData.xialiao_num ) {
      if (!formRef.value) return
      callback(new Error('超出最大量'))
    }
    callback()
  }
}

// 表单验证
const formRules = reactive<any>({
    num:[{validator:checknum,trigger: 'blur'}]
})


// 获取详情
const setFormData = async (row: Record<any, any>) => {
    let data:any = {}
  
  if(row.xialiao==1){
      data['order_id']=row.order_id
      data['detail_id']=row.id
      data['allnum']=row.num
      data['xialiao_num']=row.xialiao_num
  }else{
      data = await apiOrdersXialiaoDetail({ id: row.id,detail_id: row.detail_id})
      data['allnum']=row.num
  }

 
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    
    
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData,  }
    mode.value == 'edit' 
        ? await apiOrdersXialiaoEdit(data) 
        : await apiOrdersXialiaoAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData
})
</script>
